In this example we use built in Tab View (this is similar to using our Custom MyTab except this one is part of the system).
In this example we define three Tabs and for each we define font color when it is selected or deselected.
When Tab is selected corresponding content is shown on the screen.
MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Tab
import androidx.compose.material.TabRow
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.setContent
val colorSelected = Color.Green
val colorDeselected = Color.Unset
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val selectedTabIndex = remember { mutableStateOf(0) }
Box(Modifier.fillMaxSize()) {
TabRow(
modifier = Modifier.align(Alignment.TopStart), //Without this goes on top
selectedTabIndex = selectedTabIndex.value,
tabs = {
Tab(
selected = selectedTabIndex.value == 0,
onClick = { selectedTabIndex.value = 0 },
unselectedContentColor = Color.Green,
selectedContentColor = Color.Red
)
{
Text("Tab 0")
}
Tab(
selected = selectedTabIndex.value == 1,
onClick = { selectedTabIndex.value = 1 },
unselectedContentColor = Color.Green,
selectedContentColor = Color.Red
)
{
Text("Tab 1")
}
Tab(
selected = selectedTabIndex.value == 2,
onClick = { selectedTabIndex.value = 2 },
unselectedContentColor = Color.Green,